
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from backend.config import get_config

config = get_config()

engin = create_engine(
    config.db_url,
    echo=True
)

session = sessionmaker(
    bind=engin,
    autoflush=False,
    autocommit=False
)

def get_db():
    db = session()
    try:
        yield db
    finally:
        db.close()

Base = declarative_base()

def create_tables():
    """
    自动初始化数据库
    """
    Base.metadata.create_all(bind=engin)